<?php

namespace App\Http\Controllers\Api;

use App\Helpers\Api\MiniProgramTrait;
use App\Http\Controllers\BaseController;
use App\Jobs\NewsUrl;
use App\Models\Cookbook;
use App\Models\NewUrl;
use EasyWeChat\Kernel\Messages\Article;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use Illuminate\Http\Request;

class OfficialAccountController extends BaseController
{
    use MiniProgramTrait;

    function downloadImage($url)
    {
        $path = public_path('./images/');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $file = curl_exec($ch);
        curl_close($ch);
        $filename = time() . rand(0, 10000);
        $extendsion = pathinfo($url, PATHINFO_EXTENSION) ?: 'jpg';
        $filename .= "." . $extendsion;
        $resource = fopen($path . $filename, 'a');
        fwrite($resource, $file);
        fclose($resource);
    }

    public function test(Request $request) {


        exit;
        $app = $this->official();
        $list = $app->material->list('news');
        var_dump($list);
        exit;
        $this->validate($request, [
            'type' => 'required|in:cookbook,product',
            'id' => 'required'
        ]);
        $input = $request->input();
        $user_id = $this->getUserId();
        $this->dispatch(new NewsUrl($input['type'], $input['id'], $user_id));
    }

    public function test2() {

//
//        $file = 'https://qiniu.huangxingle.top/2020050220384162168.mp4?vframe/jpg/offset/0/width/100';
//
//        $this->downloadImage($file);
//        exit;


        $app = $this->official();
//        $aa = $app->broadcasting->sendText('hello world <img src="https://qiniu.huangxingle.top/2020050220384162168.mp4?vframe/jpg/offset/0" >', ['oqvqnwsAPVPcLO2ISX-tdjsZI9Sc', 'oqvqnwsAPVPcLO2ISX-tdjsZI9Sc']);
//        dd($aa);
//        return;
//        $aa = $app->material->uploadImage(public_path('./images/15991377142369.jpg'));
//        dd($aa);
//        exit;
        $aa = $app->broadcasting->sendNews('VyB2qNuTjcu9OWLUdip7SvP8TWiKOe0AX6RZh6jr2zE', ['oqvqnwsAPVPcLO2ISX-tdjsZI9Sc', 'oqvqnwsAPVPcLO2ISX-tdjsZI9Sc']);
        dd($aa);
//        $stats = $app->material->list('news', 0, 10);
//        dd($stats);
//        $token = $app->access_token->getToken();
//        $url = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=' . $token['access_token'];

        $article = new Article([
            'thumb_media_id' => 'VyB2qNuTjcu9OWLUdip7SpHpCU4q85RAOCWYQA7EtRs',
            'author' => 'xx',
            'title' => 'Happy Day',
            'source_url' => 'www.qq.com',
            'content' => '<p><a href="#">hello world</a> </p><p>1111</p><p><a data-miniprogram-appid="wx5952b7b55b9e4c11" data-miniprogram-path="pages/index/index" href=""><img src="http://mmbiz.qpic.cn/mmbiz_jpg/VzZeDLc6IXz4J5rfqYlwE0qtmC2wLia2icNEkeCZ7vhzmRCepTXZAIOTy8z0GbruA4hXpFugp9g9xDd1ACUXc3ow/0?wx_fmt=jpeg" alt="" data-width="null" data-ratio="NaN"></a></p>',
            'show_cover' => 0,
        ]);
        $aa = $app->material->updateArticle('VyB2qNuTjcu9OWLUdip7SvP8TWiKOe0AX6RZh6jr2zE', $article);
        dd($aa); // VyB2qNuTjcu9OWLUdip7SvP8TWiKOe0AX6RZh6jr2zE

        exit;
        $data_string = json_encode($data);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
        );
        $result = curl_exec($ch);
        dd($result);
    }
    public function handle(Request $request) {
        $app = $this->official();
        $app->server->push(function ($message) {
            \Log::info(var_export($message, true));
            switch ($message['MsgType']) {
                case 'event':
                    $MsgID = $message['MsgID'];
                    $info = NewUrl::where('msg_id', $MsgID)->first();
                    if ($info) {
                        $info->url = $message['ArticleUrlResult']['ResultList']['item']['ArticleUrl'];
                        $info->status = 1;
                        $info->save();
                    }
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });
        $response = $app->server->serve();

        // 将响应输出
        return $response->send();
    }


}
